//
//  LHDetailViewController.m
//  Knight_Tour
//
//  Created by dlios on 15-5-23.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHDetailViewController.h"
#import "LHAreaViewController.h"
#import "LHDetailView.h"
#import "MyNavgationView.h"
#import "LHNetworkHandle.h"
#import "LHDetailModel.h"
#import "VLDContextSheet.h"
#import "VLDContextSheetItem.h"
#import "UMSocial.h"

@interface LHDetailViewController ()<UIWebViewDelegate, VLDContextSheetDelegate>
@property (nonatomic, retain) NSMutableArray *detailArr;
@property (nonatomic, retain) MyNavgationView *detaliNavigation;
@property (nonatomic, retain) LHDetailModel *lhdm;
@property (nonatomic ,retain) VLDContextSheet *contextSheet;
@end

@implementation LHDetailViewController
- (void)dealloc
{
    [_detailArr release];
    [_contextSheet release];
    [_lhdm release];
    [_detaliNavigation release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createContextSheet];
    [self createNavigation];
    [self gateData];
}

// 添加导航
- (void) createNavigation
{
    self.detaliNavigation = [[MyNavgationView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _detaliNavigation.leftTarget = self;
    _detaliNavigation.leftAction = @selector(backToAreaPage);
    [_detaliNavigation creatLeftButtonWithImage:@"btn_nav_back"];
    
    [_detaliNavigation creatTitle:_detailTitleName];
    
    _detaliNavigation.rightTarget = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    _detaliNavigation.rightAction = @selector(press:);
    [_detaliNavigation creatRightButtonToShareOrCollect:@"53c7085374637" gestrue:tap];
    [tap release];
    [self.view addSubview:_detaliNavigation];
    [_detaliNavigation release];
}
- (void)backToAreaPage
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)gateData
{
    NSString *url = [NSString stringWithFormat:@"http://119.57.103.173:8080/tsp/sightWSAction!getSight.do?sightId=%@", self.myDetailID];
    [LHNetworkHandle getDataWithURL:url completion:^(id result) {
        
        self.lhdm = [[LHDetailModel alloc]initWithDic:[result objectForKey:@"sightBean"] ];
        self.lhdm.titleName = self.detailTitleName;
        
        
        UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, LHWidth, LHheight - 64)];
        [self.view addSubview:web];
        web.delegate = self;
        [web loadHTMLString:_lhdm.desc baseURL:nil];
        [web release];
    }];
}


#pragma mark - 导航分享收藏按钮实现方法
- (void) createContextSheet
{
    VLDContextSheetItem *item1 = [[VLDContextSheetItem alloc] initWithTitle: @"添加收藏"
                                                                      image: [UIImage imageNamed: @"add"]
                                                           highlightedImage: [UIImage imageNamed: @"add_highlighted"]];
    
    VLDContextSheetItem *item2 = [[VLDContextSheetItem alloc] initWithTitle: @"分享"
                                                                      image: [UIImage imageNamed: @"share"]
                                                           highlightedImage: [UIImage imageNamed: @"share_highlighted"]];
    
    self.contextSheet = [[[VLDContextSheet alloc] initWithItems: @[ item1, item2]]autorelease];
    self.contextSheet.delegate = self;
}

- (void) contextSheet: (VLDContextSheet *) contextSheet didSelectItem: (VLDContextSheetItem *) item {
    
    NSLog(@"Selected item: %@", item.title);
    if ([item.title isEqualToString:@"添加收藏"]) {
        NSLog(@"收藏全国景点详情");
        [self creatNSFileManager];
    } else {
        
        [self shareSights];
    }
}

- (void) press: (UIGestureRecognizer *) gestureRecognizer
{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        
        [self.contextSheet startWithGestureRecognizer: gestureRecognizer
                                               inView: self.view];
    }
}

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
                                 duration: (NSTimeInterval) duration
{
    [super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];
    
    [self.contextSheet end];
}


//分享
- (void)shareSights
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55603d4f67e58e768200361e"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"meinv01.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:nil];
}

-(void) creatNSFileManager
{
    // NSFileManager 文件管理器
    
    // 获取路径  缓存路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    // 创建一个文件管理器(单例)
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    // 添加文件夹
    // 拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"LHCountrySightDetail"];
    
    // 创建文件夹
    [filemanager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%@", filePath);
    NSString *strPath = [filePath stringByAppendingPathComponent:@"LHCountrySightDetail.kk"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
    
    if (arr == nil) {
        arr = [NSMutableArray array];
        [arr addObject:self.lhdm];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [a show];
        [a release];
    } else {
            int p = 0;
        
            for (LHDetailModel *m in arr) {
                if ([m.titleName isEqualToString:self.lhdm.titleName]) {
                    p = 1;
            }
        }
        
    if (p == 0) {
        
        [arr addObject:_lhdm];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        
        UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [a show];
        [a release];
        } else {
            UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [a show];
            [a release];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
